package RMI;

import java.rmi.*;
import java.rmi.registry.*;
import java.util.ArrayList;
import java.util.Set;
import java.net.*;
import java.net.UnknownHostException;
import search.SearchTuple;
import configuration.CreatXMLFile;
import main.Client;
import main.UserInfo;
/**
 * implement of the RMI
 * @author ran & barak
 *
 */
public class RemoteImp extends java.rmi.server.UnicastRemoteObject implements RemoteInt,Runnable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int      port;
    String   address;
    Registry registry;    
    Client client;//=Client.getClientInstance();
    public RemoteImp() throws RemoteException{
    	try {
			address= (InetAddress.getLocalHost()).toString();
			port=1234;
			client=Client.getClientInstance();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
	public void run(){
	    try{
	    	// create the registry and bind the name and object.
	    	registry = LocateRegistry.createRegistry( port );
            registry.rebind("remoteControlServer", this);
        }
        catch(RemoteException e){
        	System.err.println("error");
        }
	}
	/**
	 * send command to the service to add new tagging algorithm
	 * @param path location of plug in
	 * @param className - the name of plug in class 
	 * @return tag type name and list of the optional tags
	 * @throws RemoteException
	 */
	@Override
	public ArrayList<String> addPlugin(String path, String className) throws RemoteException {
		System.out.println("addPlugin");
		ArrayList<String> tagList=client.addPlugIn(path,className);
		return tagList;
	}
	
	
	/**
	 * send command to the service to create new folder
	 * @param boxName - folder name
	 * @return success or failure
	 * @throws RemoteException
	 */
	@Override
	public boolean createNewBox(String boxName) throws RemoteException {
		System.out.println("createNewBox");
		client.createNewBox(boxName);
		return false;
	}
	
	/**
	 * send command to the service to search file with certain tag
	 * in certain tag\s type\s
	 * @param tag - tag texy
	 * @param tagTypeList - list of tag type to search
	 * @return array with search result
	 * @throws RemoteException
	 */
	@Override
	public ArrayList<SearchTuple> search(String tag, ArrayList<String> tagTypeList)
			throws RemoteException {
		System.out.println("search");
		ArrayList<SearchTuple> ret=null;
		ret=client.search(tagTypeList,tag);
		return ret;
	}
	/**
	 * send command to the service to start communicate with server
	 * @throws RemoteException
	 */
	@Override
	public void startSync() throws RemoteException{
		System.out.println("startSync");	
		client.startListening();
	}
	/**
	 * send command to the service to stop communicate with server
	 * @throws RemoteException
	 */
	@Override
	public void stopSync() throws RemoteException{
		System.out.println("stopSync");
		client.stopListening();
	}
	
	/**
	 * @return from service the user email
	 * @throws RemoteException
	 */
	@Override
	public String getEmail() throws RemoteException {
		return UserInfo.getInstance().getEmail();
	}
	/**
	 * @return from service the password
	 * @throws RemoteException
	 */
	@Override
	public String getPass() throws RemoteException {
		return UserInfo.getInstance().getPass();
	}
	/**
	 * @return from service the user name
	 * @throws RemoteException
	 */
	@Override
	public String getUserName() throws RemoteException {
		return UserInfo.getInstance().getUserName();
	}
	/**
	 * @return from service all user folders
	 * @throws RemoteException
	 */
	@Override
	public Set<String> getBoxesList() throws RemoteException {
		return UserInfo.getInstance().getBoxesList();
	}
	
	/**
	 * send command to the service to share new folder with other users
	 * @param emails list with the emails of users we want to share
	 * @param str name of the folder the user want to share
	 * @throws RemoteException
	 */
	@Override
	public void shareBox(ArrayList<String> emails, String boxName) throws RemoteException{
		client.shareBox(emails,boxName);
		
	}
	
	/**
	 * send command to the service to open file
	 * @param path the file location
	 * @throws RemoteException
	 */
	public void openFile(String path)throws RemoteException{
		client.openFile(path);
	}
	
	/**
	 * send command to the service to search files with certain tags
	 * @param tag the tags we want to search
	 * @return array with search result
	 * @throws RemoteException
	 */
	public ArrayList<SearchTuple> advencedSearch(ArrayList<String> tag){
		return client.advencedSearch(tag);
		
	}
	/**
	 * send command to the service to set the algorithms that use for tagging new files
	 * @param tagTypes the tag types we want to tag with
	 * @throws RemoteException
	 */
	public void changeTagAlgoValid(ArrayList<String> tagTypes){
		client.changeTagAlgoValid(tagTypes);
	}
	public void setDbSettings(String dbUserName, String dbPassword)throws RemoteException{
		CreatXMLFile creatXMLFile = new CreatXMLFile();
		//creatXMLFile.createDbAndHibernateXml("ClientDatabase", dbUserName, dbPassword);
	}
	/**
	 * @return all tag types and their tag option
	 * @throws RemoteException
	 */
	@Override
	public ArrayList<ArrayList<String>> getPlugins() throws RemoteException {
		return client.getPlugins();
	}
	
}
